//
//  ViewController1.m
//  Movie
//
//  Created by tengyang on 15/7/22.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "ViewController1.h"
#import "DataService.h"
#import "UIImageView+WebCache.h"
#import "ImageListModal.h"
#import "ImageListCell.h"
#import "PhotoViewController.h"
#import "BaseNavController.h"

#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height

@interface ViewController1 ()

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    [self _createCollectionView];
    [self _loadData];
}

- (void)_loadData
{
    NSArray *array=[DataService getJsonDataFromFile:@"image_list.json"];
    _newsImageArray =[[NSMutableArray alloc]init];
    for (NSDictionary *dic in array) {
        ImageListModal *modal=[[ImageListModal alloc]init];
        modal.image=[dic objectForKey:@"image"];
        [_newsImageArray addObject:modal];
        
    }
}

- (void)_createCollectionView
{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kWidth-50)/4, (kWidth-50)/4);
    
    _newsCollectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:_newsCollectionView];
    _newsCollectionView.dataSource=self;
    _newsCollectionView.delegate=self;
    
    [_newsCollectionView registerClass:[ImageListCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _newsImageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageListCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modal=_newsImageArray[indexPath.row];
   

    return cell;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 20, 10);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    for (int i=0; i<_newsImageArray.count; i++) {
        ImageListModal *modal=_newsImageArray[i];
        NSString *temp=modal.image;
        [array addObject:temp];
    }
    
    PhotoViewController *vc=[[PhotoViewController alloc]init];
    vc.index=indexPath.row;
    vc.view.backgroundColor=[UIColor redColor];
    vc.imageUrlArray=array;
    BaseNavController *nav=[[BaseNavController alloc]initWithRootViewController:vc];
    
    
    [self presentViewController:nav animated:YES completion:nil];
}



@end
